package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;

@Cmd(code = CrossProtocol.C_CROSS_FEAST_PARTY_REWARD, desc = "")
public class CrossGetFeastPartyRewardCmd extends CrossCommand {
    private long userId;
    private String banquetId;
    private int rewardId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.BanquetGetPopRewardReq reqMsg = BanquetProto.BanquetGetPopRewardReq.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        banquetId = reqMsg.getBanquetId();
        rewardId = reqMsg.getRewardId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossBanquetData banquet = CrossPartyMgr.getParty(banquetId);
        if (banquet == null || CrossPartyMgr.isBanquetOver(banquet.getBanquetId())) {
            ret = GameErrorCode.E_BANQUET_IS_OVER;
            return;
        }

        Map<Integer, List<String>> rewardInfoMap = CrossBanquetConfigMgr.getBanquetFeastRewardMap();
        if (banquet.getUserCount() < Integer.valueOf(rewardInfoMap.get(rewardId).get(1))) {
            ret = GameErrorCode.E_BANQUET_FEAST_POP_CANT_REWARD;
        }
        BanquetProto.CrossFeastGetRewardRespMsg.Builder resp = BanquetProto.CrossFeastGetRewardRespMsg.newBuilder();
        resp.setRet(ret);
        resp.setPartyId(banquetId);
        resp.setRewardId(rewardId);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_PARTY_FEAST_REWARD, resp);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
